package com.interview.core.attachment.service;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.interview.core.attachment.entity.Attachment;
import com.interview.core.attachment.query.AttachmentConditionQuery;
import com.interview.core.attachment.repository.AttachmentRepository;
import com.interview.core.date.DateUtils;

@Service
public class AttachmentService {

	@Resource
	private AttachmentRepository attachmentRepository;

	public List<Attachment> dynamicQuery(
			AttachmentConditionQuery attachmentConditionQuery) {
		return attachmentRepository.dynamicQuery(attachmentConditionQuery);
	}

	public List<Attachment> findAll() {
		return attachmentRepository.findAll();
	}

	public Attachment findOne(String id) {
		return attachmentRepository.findOne(id);
	}

	public void save(Attachment attachment) {
		attachmentRepository.save(attachment);
	}

	public Attachment update(Attachment attachment) {
		return attachmentRepository.update(attachment);
	}

	public void delete(String id) {
		attachmentRepository.delete(id);
	}

	public Attachment saveFile(MultipartFile mul, String moduleName) {

		Attachment attachment = new Attachment();

		if (mul != null) {

			String relativeFileFolderStr = generateRelativeFileFolder(moduleName);
			String absoluteFileFolderStr = generateAbsoluteFileFolderByRelativeFileFolder(relativeFileFolderStr);
			try {

				File absoluteFile = new File(absoluteFileFolderStr);// 成为一个文件夹
				if (!absoluteFile.isDirectory()) {
					if (!absoluteFile.mkdirs()) {
						System.out
								.println("地址错误无法设定地址------------------------------------------------------");
					}
				}

				String saveName = UUID.randomUUID().toString();// 生成Id

				String fileName = mul.getOriginalFilename();// 拿文件原名

				String contentType = mul.getContentType();// 文件类型

				Long fileSize = mul.getSize();// 文件大小

				String extension = StringUtils.getFilenameExtension(fileName);// 拿后缀名

				String relativeFilePath = relativeFileFolderStr;// 相对路径名

				String absoluteFilePath = absoluteFileFolderStr + saveName
						+ "." + extension;// 重装安装文件名(包括根目录)

				File file = new File(absoluteFilePath);// 根据地址及文件的saveName成为一个文件

				FileCopyUtils.copy(mul.getBytes(), file);// 复制及目标地址

				attachment.setContentType(contentType);
				attachment.setExtension(extension);
				attachment.setFileName(fileName);
				attachment.setFileSize(fileSize);
				attachment.setRelativeFilePath(relativeFilePath);
				attachment.setSaveName(saveName);
				attachment.setModuleName(moduleName);
				this.save(attachment);
			} catch (IOException e) {

				e.printStackTrace();
			}

		}

		return attachment;
	}

	public void deleteFile(String id) {
		Attachment attachment = findOne(id);
		String relativeFilePath = attachment.getRelativeFilePath();
		String absolutePath = getUploadDir() + relativeFilePath;

		if (relativeFilePath != null) {
			File file = new File(absolutePath);

			file.delete();
		}

		this.delete(id);
	}

	public String generateRelativeFileFolder(String moduleName) {
		String date = DateUtils.formatDate("yyyy-MM-dd", new Date());
		// 写多张表存放头文件夹"file",可以经匹配选择存放那个文件夹
		String path = "file" + File.separator + moduleName + File.separator
				+ date + File.separator;
		return path;
	}

	public String generateAbsoluteFileFolderByRelativeFileFolder(
			String relativeFileFolder) {
		String absoluteFileFolder = getUploadDir();
		absoluteFileFolder = absoluteFileFolder + relativeFileFolder;
		return absoluteFileFolder;
	}

	public String getUploadDir() {

		String uploadDir = "D:" + File.separator + "Users" + File.separator
				+ "Jacky" + File.separator + "workspace" + File.separator
				+ "interviewproject" + File.separator + "src" + File.separator
				+ "main" + File.separator + "webapp" + File.separator
				+ "WEB-INF" + File.separator;
		return uploadDir;

	}

	public String processAbsolutePathForAttachment(Attachment attachment) {
		String absolutePath = getUploadDir() + attachment.getRelativeFilePath()
				+ attachment.getSaveName()+"."+attachment.getExtension() ;
		return absolutePath;
	}
}
